package com.homedo.odin.solution.controller.personal;

import com.homedo.core.common.base.controller.BaseController;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.ParamCheckUtil;
import com.homedo.odin.solution.contract.personal.brandowner.DataListResp;
import com.homedo.odin.solution.contract.personal.userhomepage.AddPopularityReq;
import com.homedo.odin.solution.contract.personal.userhomepage.AddUserIntroductionReq;
import com.homedo.odin.solution.contract.personal.userhomepage.AddVisitorReq;
import com.homedo.odin.solution.contract.personal.userhomepage.CountResp;
import com.homedo.odin.solution.contract.personal.userhomepage.IntroductionResp;
import com.homedo.odin.solution.contract.personal.userhomepage.UpdateUserBannerReq;
import com.homedo.odin.solution.contract.personal.userhomepage.UserInfoResp;
import com.homedo.odin.solution.contract.personal.userhomepage.UserSolListResp;
import com.homedo.odin.solution.contract.personal.userhomepage.VisitorListResp;
import com.homedo.odin.solution.service.bizservice.personal.CommonBizService;
import com.homedo.odin.solution.service.bizservice.personal.UserHomepageBizService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 已迁移，此类待删除
 *
 * @author: zhaoxudong
 * @date: 2018/3/6 15:52
 * @Description: 主页
 */
@Api(description = "个人中心-主页", tags = {"UserHomepage"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/user-homepage")
public class UserHomepageController extends BaseController<UserHomepageBizService> {

    @Autowired
    CommonBizService commonBizService;

    @ApiOperation(value = "新增访客记录", notes = "新增访客记录")
    @PostMapping("/visitor")
    public ResultData visitor(@RequestBody AddVisitorReq req, HttpServletRequest request) {
        baseService.addVisitor(request, req);
        return ResultData.newInstance();
    }

    @ApiOperation(value = "获取用户信息（头像、昵称、地址、角色、主页图）", notes = "获取用户信息（头像、地址、角色、主页图）",
            response = UserInfoResp.class)
    @GetMapping("/user/{userId}")
    public ResultData userInfo(@ApiParam("目标用户id") @PathVariable("userId") Integer userId) {
        UserInfoResp resp = baseService.findUserInfoByUserId(userId);
        logger.info("获取用户信息返回结果：{}", resp.toJson());
        return ResultData.newInstance(resp);
    }


    @ApiOperation(value = "修改主页图", notes = "修改主页图")
    @PostMapping("/banner/")
    public ResultData updateBanner(@RequestBody UpdateUserBannerReq request) {
        logger.info("修改个人主页图请求:{}", request.toJson());
        //baseService.updateBannerByUserId(request);
        return new ResultData(Meta.CODE_200);
    }

    @ApiOperation(value = "新增个人简介", notes = "新增个人简介")
    @PostMapping("/introduction")
    public ResultData introduction(@ApiParam("目标用户id") @PathVariable("userId") Integer userId,
            @RequestBody AddUserIntroductionReq request) {
        ParamCheckUtil.checkUserId(userId);
        return updateIntroduction(userId, request);
    }

    @ApiOperation(value = "修改个人简介", notes = "修改个人简介")
    @PutMapping("/introduction")
    public ResultData updateIntroduction(@ApiParam("目标用户id") @PathVariable("userId") Integer userId,
            @RequestBody AddUserIntroductionReq request) {
        ParamCheckUtil.checkUserId(userId);
        if (StringUtils.isBlank(request.getIntroduction())) {
            throw new BizException(Meta.CODE_451.getCode(), "introduction不能为空");
        }
        baseService.updateIntroductionByUserId(userId, request.getIntroduction());
        return new ResultData(Meta.CODE_200);
    }

    @ApiOperation(value = "查看个人简介", notes = "查看个人简介", response = IntroductionResp.class)
    @GetMapping("/introduction/{userId}")
    public ResultData selectIntroduction(@ApiParam("目标用户id") @PathVariable("userId") Integer userId) {
        ParamCheckUtil.checkUserId(userId);
        IntroductionResp resp = baseService.findIntroductionByUserId(userId);
        return ResultData.newInstance(resp);
    }


    @ApiOperation(value = "获取人气值和访问量", notes = "获取人气值和访问量", response = CountResp.class)
    @GetMapping("/visit-count/{userId}")
    public ResultData visitCount(@ApiParam("目标用户id") @PathVariable("userId") Integer userId) {
        ParamCheckUtil.checkUserId(userId);
        CountResp resp = baseService.findVisitCountByUserId(userId);
        return ResultData.newInstance(resp);
    }

    @ApiOperation(value = "获取访客列表", notes = "获取访客列表", response = VisitorListResp.class)
    @GetMapping("/visitor-list/{userId}")
    public ResultData visitorList(@ApiParam("目标用户id") @PathVariable("userId") Integer userId,
            @ApiParam(value = "当前页码", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "每页数量", required = true) @RequestParam Integer pageSize) {
        ParamCheckUtil.checkUserId(userId);
        ParamCheckUtil.checkPageParam(pageNum, pageSize);
        VisitorListResp resp = baseService.findVisitorList(userId, pageNum, pageSize);
        return ResultData.newInstance(resp);
    }

    @ApiOperation(value = "方案列表查询", notes = "方案列表查询", response = UserSolListResp.class)
    @GetMapping("/solList/{userId}")
    public ResultData solList(@ApiParam("目标用户id") @PathVariable("userId") Integer userId,
            @ApiParam(value = "当前页码", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "每页数量", required = true) @RequestParam Integer pageSize) {
        ParamCheckUtil.checkUserId(userId);
        ParamCheckUtil.checkPageParam(pageNum, pageSize);
        UserSolListResp resp = baseService.selectSols(userId, pageNum, pageSize);
        return ResultData.newInstance(resp);
    }

    @ApiOperation(value = "获取资料列表", notes = "获取资料列表", response = DataListResp.class)
    @GetMapping("/data-list/{userId}")
    public ResultData dataList(@ApiParam("目标用户id") @PathVariable("userId") Integer userId,
            @ApiParam(value = "当前页码", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "每页数量", required = true) @RequestParam Integer pageSize) {
        ParamCheckUtil.checkUserId(userId);
        ParamCheckUtil.checkPageParam(pageNum, pageSize);
        DataListResp resp = baseService.findDataList(userId, pageNum, pageSize);
        return ResultData.newInstance(resp);
    }


    @ApiOperation(value = "新增人气值", notes = "新增人气值")
    @PostMapping("/popularity")
    public ResultData popularity(@RequestBody AddPopularityReq req) {
        commonBizService.addPopularity(req);
        return ResultData.newInstance();
    }

}
